<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/19
 * Time: 16:21
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;


class ItemController extends BaseController
{
    /**
     * 构造函数
     * ItemController constructor.
     */
    public function __construct()
    {
        //无需进行权限检测的功能
        \Org\Auth\AuthUtil::setNoAuthFuncList([
            '/admin/item/ajaxGetAllItem'
        ]);
        parent::__construct();
    }
    /**
     * 产品项目列表
     */
    public function itemList(){
        $name = I("get.name");
        $is_enquiry = I("get.is_enquiry",0);
        $parent_cid = I("get.parent_cid",0);
        $second_cid = I("get.second_cid",0);
        $three_cid = I("get.three_cid",0);
        $status = I("get.status",0);
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $name && $params['name'] = $name;
        $status && $params['status'] = $status;
        $parent_cid && $params['parent_cid'] = $parent_cid;
        $second_cid && $params['second_cid'] = $second_cid;
        $three_cid && $params['three_cid'] = $three_cid;
        $is_enquiry && $params['is_enquiry'] = $is_enquiry;

        $result = D('Item', 'Logic')->searchItemList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $itemList = $result['itemList'];

        //生成分页
        $url = "/item/itemList?name={$name}&status={$status}&is_enquiry={$is_enquiry}&parent_cid={$parent_cid}&second_cid={$second_cid}&three_cid={$three_cid}";
        $page_html = create_page($url, $total, $page, $page_size);

        $one_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>1]);
        $two_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>2]);
        $three_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>3]);

        $this->assign('one_cate', $one_cate);
        $this->assign('two_cate', $two_cate);
        $this->assign('three_cate', $three_cate);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('name', $name);
        $this->assign('status', $status);
        $this->assign('is_enquiry', $is_enquiry);
        $this->assign('parent_cid', $parent_cid);
        $this->assign('second_cid', $second_cid);
        $this->assign('three_cid', $three_cid);

        $this->assign('itemList', $itemList);

        $this->loadFrame('itemList');
    }

    /**
     * 编辑项目
     */
    public function editItem(){
        $id = I("get.id");
        $itemInfo = [];
        if($id){
            $itemInfo = D("Resource/Item","Service")->getItemInfo(['id'=>$id]);
        }
        $one_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>1]);
        $two_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>2]);
        $three_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>3]);

        $this->assign('one_cate', $one_cate);
        $this->assign('two_cate', $two_cate);
        $this->assign('three_cate', $three_cate);
        $this->assign('itemInfo', $itemInfo);
        $this->assign('item_id', $id);

        $this->loadFrame('editItem');
    }

    /**
     * 添加&&编辑项目
     */
    public function ajaxSaveItem(){
        $data = [];
        isset($_POST['id']) && $data['id'] = intval($_POST['id']);
        isset($_POST['name']) && $data['name'] = strval($_POST['name']);
        isset($_POST['status']) && $data['status'] = intval($_POST['status']);
        isset($_POST['put_port']) && $data['put_port'] = strval($_POST['put_port']);
        isset($_POST['put_place']) && $data['put_place'] = strval($_POST['put_place']);
        isset($_POST['parent_cid']) && $data['parent_cid'] = intval($_POST['parent_cid']);
        isset($_POST['second_cid']) && $data['second_cid'] = intval($_POST['second_cid']);
        isset($_POST['three_cid']) && $data['three_cid'] = intval($_POST['three_cid']);
        isset($_POST['is_enquiry']) && $data['is_enquiry'] = intval($_POST['is_enquiry']);

        if(!$data){
           return Response(201,"无需操作");
        }
        D("Item","Logic")->saveItem($data);
    }

    /**
     * @return string
     */
    public function deleteItem(){
        $data = [];
        isset($_POST['id']) && $data['id'] = strval($_POST['id']);
        isset($_POST['status']) && $data['status'] = intval($_POST['status']);

        if(!$data){
            return Response(201,"无需操作");
        }
        D("Item","Logic")->deleteItem($data);
    }

    /**
     *
     */
    public function ajaxGetAllItem(){
        $parent_cid = I('post.parent_cid');
        $second_cid = I('post.second_cid');
        $three_cid = I('post.three_cid');
        $status = I('post.status',1);

        $data = [];
        $data['parent_cid'] = $parent_cid;
        $data['second_cid'] = $second_cid;
        $data['three_cid'] = $three_cid;
        $data['status'] = $status;

        $result = D('Item','Logic')->getAllItemList($data);

        return Response(999,'请求成功',$result);
    }
}